package viewmodel;

import java.util.List;

import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ExecutionArgParam;
import org.zkoss.bind.annotation.Init;

import admin.dao.QuestionDAO;


public class QuestionDataVM {
	private List<QuestionEditStatus> quesEditData;
	private boolean displayEdit = true;
	private String examid;
	
	@Init
	public void init(@ExecutionArgParam("examid") String examid){
		this.examid = examid;
		quesEditData = new QuestionDataModel(this.examid).convertQuesToEditData();
	}
	
	public QuestionDataVM(){
		
	}
	public List<QuestionEditStatus> getQuesEditData() {
		return quesEditData;
	}
	public void setQuesEditData(List<QuestionEditStatus> quesEditData) {
		this.quesEditData = quesEditData;
	}
	public boolean isDisplayEdit() {
		return displayEdit;
	}
	public void setDisplayEdit(boolean displayEdit) {
		this.displayEdit = displayEdit;
	}
	
	public String getExamid() {
		return examid;
	}
	public void setExamid(String examid) {
		this.examid = examid;
	}
	@Command
	public void delQues(@BindingParam("quesEditStatus")QuestionEditStatus qes){
		QuestionDAO.removeQues(qes.getQuestion());
		quesEditData.remove(qes);
		refreshRowTemplate(qes);
	}
	@Command
	public void confirm(@BindingParam("quesEditStatus") QuestionEditStatus qes){
		QuestionDAO.updateQues(qes.getQuestion());
		changeEditableStatus(qes);
		refreshRowTemplate(qes);
	}
	
	@Command
	public void changeEditableStatus(@BindingParam("quesEditStatus") QuestionEditStatus qes){
		qes.setEditingStatus(!qes.isEditingStatus());
		refreshRowTemplate(qes);
	}
	public void refreshRowTemplate(QuestionEditStatus quesEditdata){
		BindUtils.postNotifyChange(null, null, quesEditdata, "editingStatus");
	}
}
